<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="任务名称" prop="taskName"
                :rules="[
                 ]">
              <el-input v-model="inputForm.taskName" placeholder="请填写任务名称"  disabled   ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="类型" prop="type"
                :rules="[
                 ]">
                <el-select v-model="inputForm.type" placeholder="请选择"  style="width: 100%;" disabled>
                      <el-option key="1" label="注册" value="1"></el-option>
                      <el-option key="2" label="关注公众" value="2"></el-option>
                      <el-option key="3" label="活动规则" value="3"></el-option>
                      <el-option key="4" label="客服二维码" value="4"></el-option>
                      <el-option key="5" label="活动启动/关闭" value="5"></el-option>
                 </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12" v-if="inputForm.type=='1' || inputForm.type=='2'">
            <el-form-item label="邀请目标人数" prop="inviteNumber"
                :rules="[
                 ]">
              <el-input v-model="inputForm.inviteNumber" placeholder="请填写邀请目标人数"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12" v-if="inputForm.type=='5'">
            <el-form-item label="启动/关闭" prop="inviteNumber"
                :rules="[
                 ]">
                 <el-switch
                    v-model="inputForm.inviteNumber"
                    active-value="1" 
                    inactive-value="0"
                    active-color="#13ce66"
                    inactive-color="#ff4949">
                  </el-switch>
           </el-form-item>
        </el-col>
        <el-col :span="12" v-if="inputForm.type=='1' || inputForm.type=='2'">
            <el-form-item label="完成奖励金额" prop="price"
                :rules="[
                 ]">
              <el-input v-model="inputForm.price" placeholder="请填写完成奖励金额"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12" v-if="inputForm.type=='1' || inputForm.type=='2'">
            <el-form-item label="任务时间(天)" prop="taskTime"
                :rules="[
                 ]">
              <el-input v-model="inputForm.taskTime" placeholder="请填写任务时间(天)"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24" v-show="inputForm.type=='3'">
            <el-form-item label="活动规则" prop="remarks" :rules="[
                 ]">
              <WangEditor ref="contentEditor" v-model="inputForm.remarks" />
            </el-form-item>
          </el-col>
          <el-col :span="24" v-show="inputForm.type=='4'">
              <el-form-item label="客服二维码" prop="qrcode" :rules="[
                 ]">
                <el-upload ref="qrcode" v-if="visible" list-type="picture-card"
                  :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/taskConfig/taskConfig`"
                  :headers="{token: $cookie.get('token')}" :on-preview="(file, fileList) => {
                       showImg(file.url)
                    }" :on-success="(response, file, fileList) => {
                       inputForm.qrcode = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }" :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.qrcode = fileList.map(item => item.url).join('|')
                    }" :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }" multiple :limit="1" :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }" :file-list="qrcodeImageArra">
                  <i class="el-icon-plus"></i>
                </el-upload>
              </el-form-item>
            </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import WangEditor from '@/components/editor/WangEditor'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        qrcodeImageArra:[],
        inputForm: {
          id: '',
          taskName: '',
          type: '',
          inviteNumber: '',
          price: '0',
          taskTime: '',
          qrcode:'',
          remarks:''
        }
      }
    },
    components: {
      WangEditor
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建分享配置`
        } else if (method === 'edit') {
          this.title = '修改分享配置'
        } else if (method === 'view') {
          this.title = '查看分享配置'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.$refs.contentEditor.init('')
          this.qrcodeImageArra = []
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/commontaskconfig/commonTaskConfig/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.commonTaskConfig)
              this.$refs.contentEditor.init(this.inputForm.remarks)
              this.inputForm.qrcode.split("|").forEach((item) => {
                if (item.trim().length > 0) {
                  this.qrcodeImageArra.push({
                    name: decodeURIComponent(
                      item.substring(item.lastIndexOf("/") + 1)
                    ),
                    url: item,
                  });
                }
              });
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        if (this.inputForm.type == 3 || this.inputForm.type == 5){
          this.inputForm.price = 0
        }
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/commontaskconfig/commonTaskConfig/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>